﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.IdentityModel.Tokens.Jwt;
using T.ApiResults;
using T.Basic.Domain.T.Basic.Dto;
using T.Basic.Domain.T.Basic.Model;
using T.Basic.Read.API.Appliction.Command.CargoOwnerCommand;
using T.Basic.Read.API.Appliction.Command.FuelCostCommand;
using T.Basic.Read.API.Appliction.Command.LineCommand;

namespace T.Basic.Read.API.Controllers
{
    /// <summary>
    /// 读取货主信息
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]//JWT验证
    [ApiController]
    public class CargoOwnersController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator _mediator;
        public CargoOwnersController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 显示货主信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDto<CargoOwner>>> GetCargoOwnerAll([FromQuery] CargoOwnerListCommand command)
        {
            ApiResult<PageDto<CargoOwner>> result = new ApiResult<PageDto<CargoOwner>>();
            try
            {
                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//公司ID
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 导出货主文档
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet]
        public async Task<IActionResult> GetExcelName([FromQuery] CargoOwnerListCommand command)
        {
            try
            {
                IWorkbook wb = new XSSFWorkbook();
                ISheet sheet = wb.CreateSheet("货主列表");
                IRow row = sheet.CreateRow(0);
                row.CreateCell(0).SetCellValue("编号");
                row.CreateCell(1).SetCellValue("货主名称");
                row.CreateCell(2).SetCellValue("手机号");
                row.CreateCell(3).SetCellValue("货主单位");
                row.CreateCell(4).SetCellValue("联系地址");
                row.CreateCell(5).SetCellValue("驾驶证有效期");
                row.CreateCell(6).SetCellValue("驾驶证图片");
                row.CreateCell(7).SetCellValue("创建人");
                row.CreateCell(8).SetCellValue("创建时间");
                var fueList = await _mediator.Send(command);
                int index = 1;
                foreach (var item in fueList.list)
                {
                    row = sheet.CreateRow(index);
                    row.CreateCell(0).SetCellValue(item.CargoOwnerId);
                    row.CreateCell(1).SetCellValue(item.CargoOwnerName);
                    row.CreateCell(2).SetCellValue(item.CargoOwnerPhone);
                    row.CreateCell(3).SetCellValue(item.CargoOwnerUnitName);
                    row.CreateCell(4).SetCellValue(item.StartingProvince+item.StartingCity+item.StartingCounty);
                    row.CreateCell(5).SetCellValue(item.DriverLicenseDate.ToString());
                    row.CreateCell(5).SetCellValue(item.DriverLicenseImg);
                    row.CreateCell(7).SetCellValue(item.CreatedBy);
                    row.CreateCell(8).SetCellValue(item.CreatedDate.ToString());
                    index++;
                }

                //导出
                byte[] buffer;
                using (MemoryStream ms = new MemoryStream())
                {
                    wb.Write(ms);
                    buffer = ms.ToArray();
                }
                string excelMine = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
                return File(buffer, excelMine, "货主列表.xlsx");

            }
            catch (Exception)
            {

                throw;
            }


        }
    }
}
